//
//  LXUserNameEditViewController.m
//  cat
//
//  Created by imera on 16/3/15.
//  Copyright © 2016年 LX. All rights reserved.
//

#import "LXUserNameEditViewController.h"
#import "LXNavBarView.h"
#import "AFNetworking.h"
#import "MBProgressHUD+MJ.h"

@interface LXUserNameEditViewController() <LXNavBarViewDelegate>

@property (nonatomic, weak) LXNavBarView *navBarView;

@property (nonatomic, weak) UITextField *userNameTextField;

@end

@implementation LXUserNameEditViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = MainBackgroundColor;
    
    [self initSubViews];
    
}

#pragma mark - 输入框输入文字
- (void)textChange
{
    self.navBarView.rightBtn.enabled = YES;
}

- (void)initSubViews
{
    /** 导航条 */
    LXNavBarView *navBarView = [[LXNavBarView alloc]init];
    navBarView.leftTitle = @"修改昵称";
    navBarView.rightTitle = @"保存";
    navBarView.myDelegate = self;
    navBarView.rightBtn.enabled = NO;
    navBarView.frame = CGRectMake(0, 20, self.view.frame.size.width, LXNavBarHeight);
    [self.view addSubview:navBarView];
    self.navBarView = navBarView;
    
    /** 输入框 */
    UITextField *userNameEditField = [[UITextField alloc]init];
    userNameEditField.placeholder = @"请输入昵称";
    userNameEditField.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:userNameEditField];
    self.userNameTextField = userNameEditField;
    
    CGFloat userNameEditFieldX = 10;
    CGFloat userNameEditFieldY = 20 + LXNavBarHeight + 10;
    CGFloat userNameEditFieldW = self.view.frame.size.width - userNameEditFieldX * 2;
    CGFloat userNameEditFieldH = 40;
    userNameEditField.frame = CGRectMake(userNameEditFieldX, userNameEditFieldY, userNameEditFieldW, userNameEditFieldH);
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:nil];
}

#pragma mark - 导航条代理
- (void)popCurrentViewController:(LXNavBarView *)navBarView
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)rightBtnClicked:(LXNavBarView *)navBarView
{
    // 1.创建请求管理对象
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    
    // 2.发送请求
    [mgr POST:@"http://cat666.com/cat666-interface/index.php/index/alterNick" parameters:@{@"userid":@(self.user.ID), @"nick":self.userNameTextField.text} success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dict = responseObject;
        if([dict[@"successed"] integerValue] == 1){
            
            [MBProgressHUD showSuccess:@"修改昵称成功"];
            
            self.user.uname = self.userNameTextField.text;
            
            [[NSNotificationCenter defaultCenter]postNotificationName:@"userNameChange" object:nil];
            
        }else{
            [MBProgressHUD showSuccess:@"修改昵称失败"];
        }
      } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [MBProgressHUD showSuccess:@"修改昵称失败"];
      }];
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
@end
